// 当前用户选中的座位
let selectSeatData = [];

showSchedule();

function showSchedule() {
    // 获取本地存储中的排片场次_id
    const scheduleId = sessionStorage.scheduleId;
    getScheduleDataById(scheduleId)
        .then(res => {
            // console.log(res);
            if (res.code == 200) {
                seatRender(res.data.seats);
                movieInfoRender();
            }
        })

}


// 左边座位渲染
function seatRender(seatsData) {
    rowNumber.innerHTML = seatsData.map((item, index) => {
        return `<div class="row-number">${index + 1}</div>`
    }).join('')

    allSeatEle.innerHTML = seatsData.map(row => {
        return (
            `<div class="row-item">
                ${row.map(item => {
                    if (item.is_empty) {
                        return `<div class="seat-item seat-empty"></div>`
                    } else if (item.is_sold) {
                        return `<div class="seat-item seat-sold"></div>`
                    } else if (selectSeatData.some(selectItem => selectItem._id == item._id)) {
                        return `<div class="seat-item is-select seat-select" data-row="${item.row}" data-col="${item.col}" data-id="${item._id}"></div>`
                    }
                    return `<div class="seat-item is-select" data-row="${item.row}" data-col="${item.col}" data-id="${item._id}"></div>`
                }).join('')}
            </div>`
        )
    }).join('')
}

// 右边电影信息渲染
function movieInfoRender() {

}


allSeatEle.onclick = function (e) {
    if (e.target.className.includes('is-select')) {
        if (e.target.className.includes('seat-select')) {
            selectSeatData = selectSeatData.filter(item => item._id != e.target.dataset.id)
        } else {
            selectSeatData.push({
                _id: e.target.dataset.id,
                row: e.target.dataset.row,
                col: e.target.dataset.col
            })
        }
        showSchedule();
    }
}